﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UtilZ.Dotnet.Ex.Communication.Net.Telnet.Commands
{
    internal class TelnetCommandConstant
    {
        private static readonly IReadOnlyDictionary<string, string> _codeNameDic;
        static TelnetCommandConstant()
        {
            _codeNameDic = new Dictionary<string, string>()
            {
                { TSET,"设置(tset -encoding utf-8)"},
                { HELP,"帮助"},
                { EXIT,"退出"},
                { GET_TELNET_INFO,"查看Telnet信息"},
                { ENCODING,"设置编码"},

                { CLEAR_SCREEN_COMMAND,"清空屏幕命令\",\"未实现,未找着相应的命令"},
                { CLEAR_HISTORY_COMMAND,"清空历史命令"},
                { SET_HISTORY_COMMAND_CAPCITY,"设置历史命令缓存大小\",\"小于等于0无限,大于0仅缓存设置值"},
                { TERMINAL_OS_COMMAND,"指定终端交互系统\",\"2:windowsNT;4:linux/unix;6:mac os"},
            };
        }


        /// <summary>
        /// 设置
        /// </summary>
        public const string TSET = "tset";

        /// <summary>
        /// 帮助
        /// </summary>
        public const string HELP = "-h";

        /// <summary>
        /// 退出
        /// </summary>
        public const string EXIT = "-q";

        /// <summary>
        /// 获取Telnet信息
        /// </summary>
        public const string GET_TELNET_INFO = "-info";

        /// <summary>
        /// 设置编码
        /// </summary>
        public const string ENCODING = "-e";

        /// <summary>
        /// 清空屏幕命令
        /// </summary>
        public const string CLEAR_SCREEN_COMMAND = "-cs";

        /// <summary>
        /// 清空历史命令
        /// </summary>
        public const string CLEAR_HISTORY_COMMAND = "-c";

        /// <summary>
        /// 设置历史命令缓存大小
        /// </summary>
        public const string SET_HISTORY_COMMAND_CAPCITY = "-hcc";
        

        /// <summary>
        /// 指定终端交互系统
        /// </summary>
        public const string TERMINAL_OS_COMMAND = "-tos";



        /// <summary>
        /// 获取命令帮助信息
        /// </summary>
        /// <returns></returns>
        public static string GetCommandHelpInfo()
        {
            int nameMaxLength = 0, desMaxLength = 0;
            foreach (var item in _codeNameDic.Values)
            {
                if (item.Length > nameMaxLength)
                {
                    nameMaxLength = item.Length;
                }
            }

            const int SPACE = 10;
            int nameLen = nameMaxLength + SPACE;
            int desLen = desMaxLength + SPACE;
            string cmd, des;

            StringBuilder sb = new StringBuilder();

            sb.Append(TSET);
            sb.AppendLine("命令说明");

            cmd = $"{{0, -{nameLen - 2}}}";
            sb.Append(string.Format(cmd, "命令"));

            des = $"{{0, -{desLen}}}";
            sb.AppendLine(string.Format(des, "描述"));


            foreach (var kv in _codeNameDic)
            {
                cmd = $"{{0, -{nameLen}}}";
                sb.Append(string.Format(cmd, kv.Key));

                des = $"{{0, -{0}}}";
                sb.Append(string.Format(des, kv.Value));
                sb.AppendLine();
            }

            return sb.ToString();
        }
    }
}
